﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HRMS_Model;
using System.Diagnostics;



namespace HRMS.Models
{
    public class ClientsModels
    {

        public static bool AddClient(Client client)
        {
            try
            {

                using (var clientEntity = new HRMSEntities())
                {
                    clientEntity.Clients.AddObject(client);
                    clientEntity.SaveChanges();
                    return true;
                }
            }

            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static bool DeleteClient(int id)
        {
            try
            {
                using (var clientEntity = new HRMSEntities())
                {
                    clientEntity.Clients.DeleteObject(clientEntity.Clients.First(c => c.ClientId == id));
                    clientEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static List<Client> GetClients(string sortExpression, string sortDirection, int pageIndex, int pageSize, int pageCount)
        {
            using (var clientEntity = new HRMSEntities())
            {
                return clientEntity.Clients.OrderBy("it." + sortExpression + " " + sortDirection).Skip(pageIndex * pageSize).Take(pageCount * pageSize).ToList();
            }
        }
        public static int GetClientsCount()
        {
            using (var clientEntity = new HRMSEntities())
            {
                return clientEntity.Clients.Count();
            }
        }

        public static bool UpdateClient(Client client)
        {
            try
            {
                using (var clientEntity = new HRMSEntities())
                {
                    var clientToEdit = clientEntity.Clients.FirstOrDefault(c => c.ClientId == client.ClientId);
                    if (clientToEdit != null) clientToEdit.CompanyName = client.CompanyName;
                    clientEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

    }
    }


